package com.lq.util.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


/**
 * JDBC工厂<br>
 * @author 吃人的肉
 * QQ:376870344<br>
 * email:liuqingrou@163.com
 */
public class Jdbc{

	public static Logger log = Logger.getLogger(Jdbc.class);
	
	public static Connection getCon(){
		try {
			return LqDBOperator.dsWebgame.getConnection();
		} catch (SQLException e) {
			log.error(e);
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * EXEC
	 * @param sql
	 * @return
	 */
	@Deprecated
	public static int execute(String sql){
		int b=0;
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		b = jdbc.execute(sql);
		jdbc.close();
		return b;
	}
	/**
	 * EXEC
	 * @param sql
	 * @return
	 */
	public static int execute(String sql,Object... obj){
		int b=0;
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		b = jdbc.execute(sql,obj);
		jdbc.close();
		return b;
	}
	
	/**
	 * 分页
	 * @param sql
	 * @param pageNumber
	 * @param pageSize
	 * @param totalCount
	 * @return
	 */
	@Deprecated
	public static Page findPage(String sql,Integer pageNumber,Integer pageSize,Long totalCount){
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		Page page= jdbc.find(sql, pageNumber, pageSize, totalCount);
		jdbc.close();
		return page;
	}
	/**
	 * 分页《安全》<br>
	 * SQL_SERVER_2005分页:SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* from tableName
	 * @param sql
	 * @param pageNumber1 当前页数
	 * @param pageSize 一页所显示的记录数
	 * @param totalCount 总记录数
	 * @return
	 */
	public static Page findPage(String sql,Integer pageNumber,Integer pageSize,Long totalCount,Object... obj){
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		Page page= jdbc.find(sql, pageNumber, pageSize, totalCount,obj);
		jdbc.close();
		return page;
	}
	
	/**
	 * 分页《安全》<br>
	 * SQL_SERVER_2005分页:SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* from tableName
	 * @param sql
	 * @param pageNumber1 当前页数
	 * @param pageSize 一页所显示的记录数
	 * @param sqlCount 得总记录数SQL
	 * @return
	 */
	public static Page findPage(String sql,Integer pageNumber,Integer pageSize,String sqlCount,Object... obj){
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		Page page= jdbc.find(sql, pageNumber, pageSize, sqlCount,obj);
		jdbc.close();
		return page;
	}
	
	/**
	 * 查询<有BUG>
	 * @param sql
	 * @param cls
	 * @return
	 */
	@Deprecated
	public static <T> List<T> find(String sql,Class<T> cls){
		List list=null;
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		list = jdbc.find(sql,cls);
		jdbc.close();
		return list;
	}
	
	/**
	 * 查询<有BUG>
	 * @param sql
	 * @param cls
	 * @param obj
	 * @return
	 */
	public static <T> List<T> find(String sql,Class<T> cls,Object... obj){
		List list=null;
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		list = jdbc.find(sql,cls,obj);
		jdbc.close();
		return list;
	}
	
	/**
	 * 查询
	 * @param sql
	 * @return
	 */
	@Deprecated
	public static List<Map> find(String sql){
		List list=null;
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		list = jdbc.find(sql);
		jdbc.close();
		return list;
	}
	/**
	 * 查询
	 * @param sql
	 * @return
	 */
	public static List<Map> find(String sql,Object... obj){
		List list=null;
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		list = jdbc.find(sql,obj);
		jdbc.close();
		return list;
	}
	
	public static int save(Object obj){
		LqJdbcFactory jdbc=new LqJdbcFactory(Jdbc.getCon());
		int b=jdbc.save(obj);
		jdbc.close();
		return b;
	}
	public static int update(Object obj){
		LqJdbcFactory jdbc=new LqJdbcFactory(Jdbc.getCon());
		int b=jdbc.update(obj);
		jdbc.close();
		return b;
	}
	public static int delete(Object obj){
		LqJdbcFactory jdbc=new LqJdbcFactory(Jdbc.getCon());
		int b=jdbc.delete(obj);
		jdbc.close();
		return b;
	}
	
	
	/**
	 * 事务操作
	 * @param shiWu
	 * @return
	 */
	public static int shiwu(LqShiWu shiWu){
		LqJdbcFactory jdbc=new LqJdbcFactory(getCon());
		try {
			jdbc.conn.setAutoCommit(false);//禁止自动提交，设置回滚点
			shiWu.shiwu(jdbc);
			if (jdbc.shiB!=null) {
				try {jdbc.conn.rollback();} catch (SQLException e1) {
					log.error(e1);
					e1.printStackTrace();
				}
			}else {
				jdbc.conn.commit();
				jdbc.conn.setAutoCommit(true);
			}
		} catch (Exception e) {
			try {jdbc.conn.rollback();} catch (SQLException e1) {
				log.error(e1);
				e1.printStackTrace();
			}
			log.error(e);
			e.printStackTrace();
		}finally{
			if(jdbc!=null){
				jdbc.close();
			}
		}
		if (jdbc.shiB==null) {
			jdbc.shiB=1;
		}
		return jdbc.shiB;
	}

}
